package com.wanghua.mall.product.controller;

import com.wanghua.common.utils.PageUtils;
import com.wanghua.common.utils.R;
import com.wanghua.mall.product.entity.AttrEntity;
import com.wanghua.mall.product.entity.ProductAttrValueEntity;
import com.wanghua.mall.product.service.AttrService;
import com.wanghua.mall.product.vo.AttrResponseVo;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.Arrays;
import java.util.List;
import java.util.Map;


/**
 * 商品属性
 *
 * @author wanghua
 * @email 2367279431@qq.com
 * @date 2021-08-23 16:10:06
 */
@RestController
@RequestMapping("/product/attr")
public class AttrController {

    @Resource
    private AttrService attrService;

    //更新spu的规格参数
    @PostMapping("/update/{spuId}")
    public R updateAttrBySpu(@PathVariable("spuId") Long spuId,
                             @RequestBody List<ProductAttrValueEntity> productAttrValueEntityList) {
        attrService.updateAttrBySpu(spuId, productAttrValueEntityList);
        return R.ok();
    }

    //查出spu的所有规格参数
    @GetMapping("/base/listforspu/{spuId}")
    public R getBySpu(@PathVariable("spuId") Long spuId) {
        List<ProductAttrValueEntity> list = attrService.getBySpu(spuId);
        return R.ok().put("data", list);

    }


    //添加属性与分组的关联
    @PutMapping("/attrGroup/relation/add/{attrGroupId}")
    public R addAttrAttrGroupRelation(@RequestBody Long[] attrIds,
                                      @PathVariable("attrGroupId") Long attrGroupId) {
        attrService.addAttrAttrGroupRelation(attrIds, attrGroupId);
        return R.ok();
    }


    //根据分组id获取所有未关联分组的基本属性列表（带分页）
    @GetMapping("/attrGroup/not/relation/{attrGroupId}")
    public R getNotGroupAttr(@RequestParam Map<String, Object> params,
                             @PathVariable("attrGroupId") Long attrGroupId) {
        PageUtils page = attrService.getNotGroupAttr(params, attrGroupId);
        return R.ok().put("page", page);
    }

    //根据属性分组id获取属性分组的关联的所有属性
    @GetMapping("/attrGroup/relation/{attrGroupId}")
    public R getAttrByAttrGroupId(@PathVariable("attrGroupId") Long attrGroupId) {
        List<AttrEntity> attr = attrService.getAttrByAttrGroupId(attrGroupId);
        return R.ok().put("items", attr);
    }

    //批量移除属性与属性分组的关联(将属性的属性分组id字段置为0)
    @PutMapping("/attrGroup/relation/delete")
    public R deleteAttrAttrGroupRelation(@RequestBody Long[] attrIds) {
        attrService.deleteAttrAttrGroupRelation(attrIds);
        return R.ok();
    }


    /**
     * 根据分类id获取基本属性列表
     */
    @GetMapping("/base/list/{categoryId}")
    public R baseListByCategoryId(@RequestParam Map<String, Object> params,
                                  @PathVariable("categoryId") Long categoryId) {
        PageUtils page = attrService.baseListByCategoryId(params, categoryId);
        return R.ok().put("page", page);
    }

    /**
     * 根据分类id获取销售属性列表
     */
    @GetMapping("/sale/list/{categoryId}")
    public R saleListByCategoryId(@RequestParam Map<String, Object> params,
                                  @PathVariable("categoryId") Long categoryId) {
        PageUtils page = attrService.saleListByCategoryId(params, categoryId);
        return R.ok().put("page", page);
    }

    /**
     * 列表
     */
    @GetMapping("/list")
    public R list(@RequestParam Map<String, Object> params) {
        PageUtils page = attrService.queryPage(params);
        return R.ok().put("page", page);
    }


    /**
     * 查询属性详细的信息
     */
    @GetMapping("/info/{id}")
    public R info(@PathVariable("id") Long id) {
        AttrResponseVo attr = attrService.infoDetail(id);
        return R.ok().put("attr", attr);
    }

    /**
     * 保存
     */
    @PostMapping("/save")
    public R save(@RequestBody AttrEntity attr) {
        attrService.save(attr);
        return R.ok();
    }

    /**
     * 修改
     */
    @PutMapping("/update")
    public R update(@RequestBody AttrEntity attr) {
        attrService.updateById(attr);
        return R.ok();
    }

    /**
     * 删除
     */
    @DeleteMapping("/delete")
    public R delete(@RequestBody Long[] ids) {
        attrService.removeByIds(Arrays.asList(ids));
        return R.ok();
    }


//    @GetMapping("/test")
//    public void test() {
//        List<AttrEntity> list = attrService.list(new QueryWrapper<AttrEntity>().eq("category_id", 225).ne("attr_group_id", 0));
//        list.forEach(System.out::println);
//    }


}
